from time import sleep

from playwright.sync_api import Page

from service.ResDataService import get_end_data
from service.StatusEnum import StatusEnum
from tools.Result import success_result
from tools.HtmlPageService import get_selector_text
response_data = {}
def handle_response(response):
    """响应处理器（带数据收集功能）"""
    global response_data
    # 记录所有响应URL

    if "queryBiddingbillsDetailfront" in response.url:
        response_data = response.json()




def get_html_detail(page: Page,detail_url:str):
    global response_data
    # 清空历史数据
    response_data = {}
    page.on("response", handle_response)
    page.goto(detail_url, wait_until="networkidle",
              timeout=15000)


    start_price= get_selector_text(page,".project-info .name")

    print(response_data)
    return get_result(start_price,response_data)


def get_result(start_price,data):
    list_data = data.get("result", {}).get("biddingSuppliers",[])
    current_price = list_data[0]["lasttotalvalue"] if list_data else ""
    status=data.get("result", {}).get("biddingbills",{}).get("billstate","")
    print(StatusEnum.get_tjyz_status(status))
    return success_result("获取成功",get_end_data(start_price,current_price,StatusEnum.get_tjyz_status(show_status=status)))
